package interfaces.hud.targetInfos;

import logic.common.player.Player;
import logic.ships.hunter.Hunter;
import settings.GraphicSettings;

import com.jme.image.Texture;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;

import fileHandling.TextureLoader;

public class AimPointer extends Node {
	
	private static final long serialVersionUID = 1L;
	
	private final String POINTER_PATH = "data/textures/hud/aimPointer.png";
	private final float SIZE = 15f;
	
	private Quad pointerQuad;
	private boolean isEnabled, isVisible;
	
	private AimPointerController controller;
	
	private Hunter hunter;
	
	private Player player;
	
	public AimPointer(Player player) {
		super("AimPointer");
		this.player = player;
		
		controller = new AimPointerController(this);
		
		pointerQuad = new Quad("Pointer Quad", SIZE, SIZE);
		pointerQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		
		BlendState bs = DisplaySystem.getDisplaySystem().getRenderer().createBlendState();
		bs.setBlendEnabled(true);
		bs.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
		bs.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
		bs.setTestEnabled(true);
		bs.setTestFunction(BlendState.TestFunction.GreaterThan);
		pointerQuad.setRenderState(bs);
		
		TextureState texState = TextureLoader.getTextureState(POINTER_PATH);
		GraphicSettings graphics = GraphicSettings.get();
		texState.getTexture().setMagnificationFilter(Texture.MagnificationFilter.Bilinear);
		texState.getTexture().setMinificationFilter(graphics.getMinificationFilter());
		texState.getTexture().setAnisotropicFilterPercent(graphics.getAnisotrop());
		pointerQuad.setRenderState(texState);
		
		pointerQuad.setLightCombineMode(LightCombineMode.Off);
		pointerQuad.updateRenderState();
	}
	
	public void setHunter(Hunter hunter) { this.hunter = hunter; }
	
	public Hunter getHunter() { return hunter; }
	
	public Player getPlayer() { return player; }
	
	public void enablePointer(boolean enable) {
		if(isEnabled == enable) return;
		
		if(isEnabled && !enable) {
			detachChild(pointerQuad);
			removeController(controller);			
		} else if(!isEnabled && enable) {
			attachChild(pointerQuad);
			addController(controller);
		}
		
		isEnabled = enable;
	}
	
	public void setVisible(boolean vis) {
		if(vis == isVisible) return;
		
		if(vis && !isVisible) attachChild(pointerQuad);
		else if(!vis && isVisible) detachChild(pointerQuad);
		
		isVisible = vis;
	}
}